package Panels;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import Runables.Run_principal;

import componentes.ConexionBD;
import componentes.labtext;
import componentes.panelconfondo;

public class PanelGSala extends panelconfondo implements ActionListener {
	GridBagConstraints gbc;
	JButton bcontinuar,breset;
	int panel;
	labtext idsala;
	labtext nombre;
	labtext idcine;
	labtext entresuelo;
	labtext principal;
	labtext lateral;
	labtext preEntresuelo;
	labtext preLateral;
	labtext prePrincipal;
	JLabel numemp;
	JComboBox cbemp;
	
	
	String consultaempleados = new String();
	

public PanelGSala(){
	super.setimagen("/recursos/cine1.jpg");
	panel = 1;
	cbemp = new JComboBox();
	cbemp.setPreferredSize(new Dimension(130, 20)); 
	this.dibujar();

	
	idsala.setText("Automatico");
	idsala.setEditable(false);
	
	if(Run_principal.user.getTipo().equals("Admin")){
		consultaempleados = "select nombre from trabajador";
		cargaempleados();
	}
	
	if(Run_principal.user.getTipo().equals("Director")){
		consultaempleados= "select nombre from trabajador where idcine="+Run_principal.user.getIdcine()+" and tipo like \"Salas\"";
		System.out.println(consultaempleados);
		cargaempleados();
		idcine.setText(Run_principal.user.getIdcine()+"");
		idcine.setEditable(false);
	}
	
}
	
public PanelGSala(String idSala){
	super.setimagen("/recursos/cine1.jpg");
	panel = 2;
	this.dibujar();
	
	
	
}


public void dibujar(){
	
	this.setLayout(new GridBagLayout());
	idsala = new labtext(" Id Sala : ",2,1,4);
    gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(idsala,gbc);
	
	nombre = new labtext(" Nombre : ",1,3,15);
    gbc = new GridBagConstraints(0, 2, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(nombre,gbc);
	
	idcine = new labtext(" Id Cine : ",2,1,4);
    gbc = new GridBagConstraints(0, 3, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(idcine,gbc);
	
	
	gbc = new GridBagConstraints(0, 4, 0, 0, 0, 0,
			GridBagConstraints.FIRST_LINE_END, GridBagConstraints.WEST, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(cbemp, gbc);
	
	numemp = new JLabel(" Encargado :");
	numemp.setOpaque(true);
    gbc = new GridBagConstraints(0, 4, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(numemp,gbc);
	
	
	
	entresuelo = new labtext(" Entresuelo : ",2,1,3);
    gbc = new GridBagConstraints(0, 5, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(entresuelo,gbc);
	
	preEntresuelo = new labtext(" precio : ",0,1,5);
    gbc = new GridBagConstraints(0, 6, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(preEntresuelo,gbc);
	
	principal = new labtext(" Principal : ",2,1,3);
    gbc = new GridBagConstraints(0, 7, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(principal,gbc);
	
	prePrincipal = new labtext(" Pecio : ",0,1,5);
    gbc = new GridBagConstraints(0, 8, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(prePrincipal,gbc);
	
	lateral = new labtext(" Lateral : ",2,1,3);
    gbc = new GridBagConstraints(0, 9, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(lateral,gbc);
	
	preLateral = new labtext(" Precio : ",0,1,5);
    gbc = new GridBagConstraints(0, 10, 1, 1, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
					5, 5, 5, 5), 0, 0);
	this.add(preLateral,gbc);
	
	bcontinuar = new JButton(" Continuar ");
    gbc = new GridBagConstraints(0, 11, 0, 0, 0, 0,
			GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
					5, 5, 5, 5), 0, 0);
    bcontinuar.addActionListener(this);
	this.add(bcontinuar, gbc);
   
	breset = new JButton(" Resetear ");
    gbc = new GridBagConstraints(0, 11, 0, 0, 0, 0,
			GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE, new Insets(
					5, 5, 5, 5), 0, 0);
    breset.addActionListener(this);
	this.add(breset, gbc);
	
	
}
@Override
public void actionPerformed(ActionEvent e) {
	
	if(panel==1){
		if (e.getSource() == bcontinuar && this.validar()==true){
			
			String consulta="INSERT INTO sala (nombre,idcine,entresuelo,principal,lateral,numemp,preEntresuelo,prePrincipal,preLateral)VALUES(\""+
		       this.nombre.getText()
              +"\",\""
		       + this.idcine.getText()
	           +"\",\""
		       + this.entresuelo.getText()
		       +"\",\""
		       + this.principal.getText()
		        +"\",\""
		       + this.lateral.getText()
		        +"\",\""
		        +  this.cbemp.getSelectedItem()
		        +"\",\""
		       + this.preEntresuelo.getText()
		        +"\",\""
		        + this.prePrincipal.getText()
		        +"\",\""
			  + this.preLateral.getText()+"\")";
		        	System.out.println(consulta);
		        	
		        	try{
						componentes.ConexionBD.executeUpdateQuery(consulta);
						JOptionPane.showMessageDialog(this,
							    "Base de datos actializada correctamente", 
							    "Message",
							    JOptionPane.INFORMATION_MESSAGE);

					}catch(Exception ex){		
						System.out.println(ex);
						JOptionPane.showMessageDialog(this,
							    "Error al actualizar los datos en la base de datos",
							    "Mensaje Error",
							    JOptionPane.ERROR_MESSAGE);
					} 	
			
			
		}
	}
}


public boolean validar(){
	
	if(
	   this.nombre.validar()==true &&	
	   this.entresuelo.validar()==true &&
	   this.preEntresuelo.validar()==true &&
	   this.principal.validar()==true &&
	   this.prePrincipal.validar()==true &&
	   this.lateral.validar()==true &&
	   this.preLateral.validar()==true) {
		return true;
	}
	JOptionPane.showMessageDialog(this,
		    "Hay datos mal introducidos",
		    "Mensaje Error",
		    JOptionPane.ERROR_MESSAGE);
	return false;
}

public void cargaempleados(){
	


	try {
		ResultSet rs = ConexionBD.executeQuery(consultaempleados);
	    
		while(rs.next()){
	    	cbemp.addItem(rs.getString(1));
	    }
	}catch(Exception e){
		e.printStackTrace();
	}
}
}


